package com.weai.portal.service.impl;

import com.weai.portal.entity.AiBooks;
import com.weai.portal.repository.AiBooksRepository;
import com.weai.portal.service.AiBooksService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * AI清单服务实现类
 */
@Service
@Slf4j
public class AiBooksServiceImpl implements AiBooksService {

    @Autowired
    private AiBooksRepository aiBooksRepository;

    @Override
    public List<AiBooks> getAllAiBookss() {
        log.debug("开始获取所有AI清单");
        try {
            List<AiBooks> aiBooks = aiBooksRepository.findAll();
            log.info("成功获取所有AI清单，数量: {}", aiBooks.size());
            return aiBooks;
        } catch (Exception e) {
            log.error("获取所有AI清单失败", e);
            throw e;
        }
    }

    @Override
    public List<AiBooks> searchByTitle(String title) {
        log.debug("开始搜索AI清单，标题关键词: {}", title);
        try {
            List<AiBooks> aiBooks = aiBooksRepository.searchByTitle(title);
            log.info("成功搜索AI清单，标题关键词: {}，数量: {}", title, aiBooks.size());
            return aiBooks;
        } catch (Exception e) {
            log.error("搜索AI清单失败，标题关键词: {}", title, e);
            throw e;
        }
    }

    @Override
    public AiBooks getAiBooksById(Long id) {
        log.debug("开始获取AI清单详情，ID: {}", id);
        try {
            AiBooks aiBooks = aiBooksRepository.findById(id)
                    .orElseThrow(() -> {
                        log.error("未找到AI清单，ID: {}", id);
                        return new RuntimeException("AiBooks not found with id: " + id);
                    });
            log.info("成功获取AI清单详情，ID: {}", id);
            return aiBooks;
        } catch (Exception e) {
            log.error("获取AI清单详情失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public AiBooks createAiBooks(AiBooks aiBooks) {
        log.debug("开始创建AI清单，标题: {}", aiBooks.getTitle());
        try {
            AiBooks savedAiBooks = aiBooksRepository.save(aiBooks);
            log.info("成功创建AI清单，ID: {}, 标题: {}", savedAiBooks.getId(), savedAiBooks.getTitle());
            return savedAiBooks;
        } catch (Exception e) {
            log.error("创建AI清单失败，标题: {}", aiBooks.getTitle(), e);
            throw e;
        }
    }

    @Override
    @Transactional
    public AiBooks updateAiBooks(Long id, AiBooks AiBooks) {
        log.debug("开始更新AI清单，ID: {}", id);
        try {
            AiBooks existingAiBooks = getAiBooksById(id);
            
            existingAiBooks.setTitle(AiBooks.getTitle());
            existingAiBooks.setDescript(AiBooks.getDescript());
            existingAiBooks.setUrlAddress(AiBooks.getUrlAddress());
            existingAiBooks.setUpdater(AiBooks.getUpdater());
            
            AiBooks updatedAiBooks = aiBooksRepository.save(existingAiBooks);
            log.info("成功更新AI清单，ID: {}", id);
            return updatedAiBooks;
        } catch (Exception e) {
            log.error("更新AI清单失败，ID: {}", id, e);
            throw e;
        }
    }

    @Override
    @Transactional
    public void deleteAiBooks(Long id) {
        log.debug("开始删除AI清单，ID: {}", id);
        try {
            aiBooksRepository.deleteById(id);
            log.info("成功删除AI清单，ID: {}", id);
        } catch (Exception e) {
            log.error("删除AI清单失败，ID: {}", id, e);
            throw e;
        }
    }
} 